package com.example.back.Repository;

import com.example.back.Entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    Product findProductByProductId(long productId);

    Page<Product> findAllByProductSold(Pageable pageable, boolean productSold);

    Page<Product> findAllByBelongToId(Pageable pageable, long belongToId);//通过所有者查询全部产品

    Page<Product> findAllByTagContains(Pageable pageable, String tagName);

    Page<Product> findAllByProductSoldAndProductNameContains(Pageable pageable, boolean productSold, String PNC);

    Page<Product> findAllByTagContainsAndProductNameContains(Pageable pageable, String TagContains, String ProductNameContains);

    Page<Product> findAll(Pageable pageable);

    boolean deleteProductByProductId(long productId);

    boolean deleteAllByBelongToId(long belongToId);

    boolean deleteAllByProductIdIn(long[] productIds);

    @Transactional
    @Modifying
    @Query(value = "update Product p set p.productLevel=:level where p.productId=:productId")
    void setProductLevelByProductId(double level, long productId);

    @Transactional
    @Modifying
    @Query(value = "update Product p set p.productSold=true where p.productId=:productId")
    void setProductSoldByProductId(long productId);

    @Transactional
    @Modifying
    @Query(value = "update Product p set p.productStock=:newStock where p.productId=:productId")
    void updateProductStockByProductId(long productId,int newStock);

}
